package com.sfh.dog.ui;


import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

import com.sfh.dog.R;
import com.sfh.dog.entity.User;
import com.sfh.dog.ui.base.BaseActivity;

/***
 * 用户登录与注册
 * 
 */
public class UserRegActivity extends BaseActivity
{

	private EditText etNumber;
	private EditText etPassWord;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_reg_activity);
		find();
	}

	private void find()
	{
		etNumber = (EditText) findViewById(R.id.etNumber);
		etPassWord = (EditText) findViewById(R.id.etPassword);
		// 登录
		findViewById(R.id.btLogin).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				User entity = getEntity();
				if (null != entity)
				{
				    entity.login(UserRegActivity.this, new SaveListener()
	                {
	                    
	                    @Override
	                    public void onSuccess()
	                    {
	                       finish();
	                        
	                    }
	                    
	                    @Override
	                    public void onFailure(int arg0, String arg1)
	                    {
	                        // TODO Auto-generated method stub
	                        
	                    }
	                });
				}

			}
		});
		// 注册
		findViewById(R.id.btReg).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
			    BmobUser bmobUser = getEntity();
			    bmobUser.signUp(UserRegActivity.this, new SaveListener()
                {
                    
                    @Override
                    public void onSuccess()
                    {
                       finish();
                        
                    }
                    
                    @Override
                    public void onFailure(int arg0, String arg1)
                    {
                        
                    }
                });

			}
		});
	}


	/***
	 * 获取数据
	 * 
	 * @return
	 */
	private User getEntity()
	{
		String number = etNumber.getText().toString();
		String password = etPassWord.getText().toString();
//		if (StringUtils.isNullValueOrEmpty(number)
//				|| StringUtils.isNullOrEmpty(password))
//		{
//			showToast("登录填写不完整");
//			return null;
//		}

		User userEntity = new User();
		userEntity.setUsername(number);
		userEntity.setPassword(password);
		return userEntity;
	}

}
